package com.panshi.controller;

import com.panshi.entity.AppUserT;
import com.panshi.result.R;
import com.panshi.service.UserClientService;
import com.panshi.utils.AdminInfo;
import com.panshi.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AdminUserController {

    @Autowired
    private UserClientService userClientService;

    @Autowired
    private AdminInfo adminInfo;

    @GetMapping("/user")
    public String user(){
        return "";
    }

    @GetMapping("/user/list")
    public String memberList(HttpServletRequest request, Model model, HttpSession session){
        adminInfo.initialize(request,model,session,"list");
        return "user/list";
    }

    @GetMapping("/user/del")
    public String userDel(HttpServletRequest request, Model model, HttpSession session){
        adminInfo.initialize(request,model,session,"del");
        return "user/del";
    }

    @GetMapping("/user/edit/{id}")
    public String userEdit(@PathVariable int id, HttpServletRequest request, Model model){
        System.out.println(id);
        AppUserT byid = userClientService.byid(id);
        model.addAttribute("appuser",byid);

        return "user/edit";
    }


    @GetMapping("/user/password")
    public String userPassword(){
        return "user/password";
    }

    @PostMapping("/user/add")
    @ResponseBody
    public R userAdd(HttpServletRequest request){
        String userid = request.getParameter("userid");
        String userpass = request.getParameter("userpass");

        System.out.println(userpass+"  "+userid);
        R r = userClientService.userUpdate(new AppUserT().setUserPassword(MD5Util.getMD5Str(userpass)).setUserId(Integer.valueOf(userid)));
        return r;
    }

    @GetMapping("/user/upd")
    public String userUpd(HttpServletRequest request,Model model,HttpSession session){
        adminInfo.initialize(request,model,session,"upd");
        return "user/upd";
    }


    @GetMapping("/user/delete")
    @ResponseBody
    public R userDelete(HttpServletRequest request){
        R r = new R();
        String id = request.getParameter("id");
        if(id==null){
            R.error();
        }
        String type = request.getParameter("type");
        System.out.println("删除方法------------》"+id);
        Boolean del = userClientService.memberStop(Integer.valueOf(id),type);
        if(del){
            r.setCode(201);
            r.setSuccess(false);
            r.setMessage("操作成功");
            return r;
        }else{
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("操作失败");
            return r;
        }
    }
}
